<?php
class MemberAction extends MbaseAction {
	public $tbname = 'Member';
	public $obj = '';
	public $mpk = 'member_id';
	public function index() {
		if (!isset ($_SESSION['username']))
			redirect(U('Member/login'));
		$morderform = M('Orderform');
		$map['member_id'] = $_SESSION['member_id'];
		//交易记录
		$dealnum = $morderform->where($map)->count();
		$this->assign('order_all_num', $dealnum);
		$map['progress'] = 1;
		//待付款订单
		$dealnum = $morderform->where($map)->count();
		$this->assign('order_unpay_num', $dealnum);
		//等待评价交易
		$map['progress'] = 4;
		$dealnum = $morderform->where($map)->count();
		$this->assign('order_payed_num', $dealnum);
		$map['is_comment'] = 0;
		$dealnum = $morderform->where($map)->count();
		$this->assign('order_uncomment_num', $dealnum);
		//购物车
		$cartinfo = cookie('mycart');
		$cartinfo = json_decode(stripcslashes($cartinfo),true);
		$goodlist = $cartinfo['good'];
		$this->assign('cart_num', count($goodlist));
		
		//佣金
		$md= D('Divide');
		$map = array('dmember_id' => $map['member_id']);
		//总佣金
		$divide['all'] = $md->where($map)->sum('fee');
		if(empty($divide['all'])) $divide['all'] = 0;
		//总佣金结算完
		$map['is_settled'] = 1;
		$divide['ok'] = $md->where($map)->sum('fee');
		if(empty($divide['ok'])) $divide['ok'] = 0;
		$divide['unok'] = $divide['all'] - $divide['ok'];
		$divide['rate'] = $this->getConfig('rate');
		
		//点击数
		$mc = M('Divide_click');
		$map = array('dmember_id' => $map['member_id']);
		$divide['click'] = $mc->where($map)->sum('click');
		if(empty($divide['click'])) $divide['click'] = 0;
		$this->assign('divide',$divide);
		$this->setSeo('会员中心');
		$this->display();
	}
	public function promote(){
		//佣金
		$md= D('Divide');
		$map = array('dmember_id' => $map['member_id']);
		//总佣金
		$divide['num'] = $md->where($map)->count();
		if(empty($divide['num'])) $divide['all'] = 0;
		$divide['all'] = $md->where($map)->sum('fee');
		if(empty($divide['all'])) $divide['all'] = 0;
		//总佣金结算完
		$map['is_settled'] = 1;
		$divide['ok'] = $md->where($map)->sum('fee');
		if(empty($divide['ok'])) $divide['ok'] = 0;
		$divide['unok'] = $divide['all'] - $divide['ok'];
		$divide['rate'] = $this->getConfig('rate');
		
		//点击数
		$mc = M('Divide_click');
		$map = array('dmember_id' => $map['member_id']);
		$divide['click'] = $mc->where($map)->sum('click');
		if(empty($divide['click'])) $divide['click'] = 0;
		$this->assign('divide',$divide);
		
		//结算记录
		$ms = M('Settle_note');
		$settlelist = $ms->where($map)->select();
		$this->assign('settlelist',$settlelist);
		$this->setSeo('我的推广');
		$this->display();
	}
	public function myorder() {
		if (!isset ($_SESSION['username']))
			redirect(U('Member/login'));
		$morderform = D('Orderform');
		$map['member_id'] = $_SESSION['member_id'];
		$progress = isset ($_GET['progress']) ? $_GET['progress'] : 0;
		if ($progress)
			$map['progress'] = $progress;
		import("ORG.Util.Page"); //导入分页类
		$count = $morderform->where($map)->count();
		$p = new Page($count, 10);
		$list = $morderform->relation(true)->where($map)->limit($p->firstRow . ',' . $p->listRows)->order('orderform_id desc')->findAll();
		$page = $p->mshow(1);
		$this->assign('listcount', $count);
		$this->assign("page", $page);
		$this->assign('datalist', $list);
		$this->setSeo('我的订单');
		$this->display();
	}
	public function login() {
		if (!$this->isPost()) {
			//if(isset($_SESSION['username'])) redirect(U('Member/index'));
			$this->display();
		} else {
			//echo cookie('jumpurl');
			$jumpurl = cookie('jumpurl');
			$username = isset ($_POST['username']) ? trim($_POST['username']) : '';
			$password = isset ($_POST['password']) ? trim($_POST['password']) : '';
			if (strlen($username) < 6 || strlen($username) > 14) {
				$msg = "用户名为6到14个字符组成";
				$this->error($msg, 'username');
				return;
			}
			$mmember = D('Member');
			$condition['username'] = $username;
			$temp = $mmember->where($condition)->find();
			if (!$temp) {
				$msg = "用户名或密码错误";
				$this->error($msg, 'username');
			}
			if (strlen($password) < 6 || strlen($password) > 16) {
				$msg = "密码为6到16个字符组成";
				$this->error($msg, 'password');
				return;
			}
			if (md5($password) != $temp['password']) {
				$msg = "密码错误";
				$this->error($msg, 'password');
				return;
			}
			$verify = isset ($_POST['verify']) ? trim($_POST['verify']) : '';
			if (md5($verify) != Session :: get('verify')) {
				$msg = "请填写正确的验证码";
				$this->error($msg, 'verify');
				return;
			}
			Session :: set('usertype', 'Member');
			Session :: set('member_id', (int) $temp['member_id']);
			Session :: set('username', $username);
			if ($jumpurl == '') {
				$this->success('登陆成功', U('Index/index'));
				//redirect(U('Member/index')); 
			} else {
				$this->success('登陆成功', $jumpurl);
				//redirect($jumpurl); 
			}
		}
	}
	public function logout() {
		$this->assign('jumpUrl', U('login'));
		$mmember = M('Member');
		$map['member_id'] = $_SESSION['member_id'];
		$data['last_ip'] = get_client_ip();
		$data['last_visit'] = time();
		$mmember->where($map)->save($data);
		Session :: clear();
		Session :: destroy();
		redirect(U('Index/index'));
	}
	public function comment() {
		if (!$this->isPost()) {
			$is_show = isset ($_GET['comment_id']) ? $_GET['comment_id'] : 0;
			$good_id = isset ($_GET['good_id']) ? $_GET['good_id'] : 0;
			if($good_id){
				$mgood = M('Good');
				$gmap['good_id'] = $good_id;
				$goodname = $mgood->where($gmap)->getField('name');
				$this->assign('goodname',$goodname);
			}
			if ($is_show) {
				$map['member_id'] = Session :: get('member_id');
				$map['orderform_id'] = isset ($_GET['orderform_id']) ? $_GET['orderform_id'] : 0;
				$map['good_id'] = $good_id;
				$mc = M('Comment');
				$datainfo = $mc->where($map)->find();
				$this->assign('datainfo', $datainfo);
			}
			$this->display('Member:b_comment');
		} else {
			$mc = M('Comment');
			$data['comment_id'] = isset ($_POST['comment_id']) ? trim($_POST['comment_id']) : 0;
			$data['member_id'] = Session :: get('member_id');
			$data['orderform_id'] = isset ($_POST['orderform_id']) ? trim($_POST['orderform_id']) : 0;
			$data['good_id'] = isset ($_POST['good_id']) ? trim($_POST['good_id']) : 0;
			$data['detail'] = isset ($_POST['detail']) ? trim($_POST['detail']) : '';
			$data['level'] = isset ($_POST['level']) ? trim($_POST['level']) : 1;
			$data['progress'] = 0;
			$data['reply'] = '';
			$data['created'] = time();
			$data['changed'] = time();
			$data['status'] = 1;
			$data['grade'] = 1;
			$data['user_id'] = 1;
			$data['oid'] = 1;
			if ($data['comment_id']) {
				$cmap['comment_id'] = $data['comment_id'];
				$data['reply'] = $mc->where($cmap)->getField('reply');
				$mc->where($cmap)->save($data);
			} else {
				if ($mc->add($data)) {
					$bmap['orderform_id'] = $data['orderform_id'];
					$bmap['good_id'] = $data['good_id'];
					$mo = M('Orderform_good');
					$mo->where($bmap)->setField('is_comment', 1);
				}
				$ocmap['orderform_id'] = $data['orderform_id'];
				$ocmap['is_comment'] = 0;
				$isallcomment = $mo->where($ocmap)->count();
				if (!$isallcomment) {
					$morder = M('Orderform');
					$morder->where('orderform_id=' . $ocmap['orderform_id'])->setField('is_comment', 1);
				}
			}

			//$this->assign('jumpUrl', U('Orderform/view?orderform_id=' . $data['orderform_id']));
			$this->success('评价成功',1);
			//redirect(U('Member/comment?comment_id=1&orderform_id='.$data['orderform_id'].'&good_id='.$data['good_id']));
		}
	}
	public function info() {
		$mmap['member_id'] = Session :: get('member_id');
		$ispost = 0;
		if ($this->isPost()) {
			$ispost = 1;
			$data = array ();
			$mmap['member_id'] = Session :: get('member_id');
			$data['realname'] = isset ($_POST['realname']) ? trim($_POST['realname']) : '';
			$data['sex'] = isset ($_POST['sex']) ? trim($_POST['sex']) : 1;
			$data['birth'] = isset ($_POST['birth']) ? trim($_POST['birth']) : '1970-1-1';
			$data['province'] = isset ($_POST['province']) ? trim($_POST['province']) : '';
			$data['city'] = isset ($_POST['city']) ? trim($_POST['city']) : '';
			$data['county'] = isset ($_POST['county']) ? trim($_POST['county']) : '';
			$data['address'] = isset ($_POST['address']) ? trim($_POST['address']) : '';
			$data['postcode'] = isset ($_POST['postcode']) ? trim($_POST['postcode']) : '';
			$data['mobile'] = isset ($_POST['mobile']) ? trim($_POST['mobile']) : '';
			$data['tel'] = isset ($_POST['tel']) ? trim($_POST['tel']) : '';
			$data['paycode'] = isset ($_POST['paycode']) ? trim($_POST['paycode']) : '';
			$mcontent = M('Member_content');
			$mcontent->where($mmap)->save($data);
			Session :: set('ok', 'Member');
			redirect(U('Member/info'));
			return;
		}
		$this->assign('ispost', $ispost);
		$mmember = D('Member');
		$datainfo = $mmember->relation('content')->where($mmap)->find();
		$this->assign('datainfo', $datainfo);
		$isok = isset ($_SESSION['ok']);
		Session :: set('ok', null);
		$this->setSeo('个人信息');
		$this->display();
		if ($isok)
			echo '<script> alert("修改成功"); </script>';
	}
	public function pwd() {
		$map['member_id'] = Session :: get('member_id');
		$mmember = D('Member');
		$datainfo = $mmember->where($map)->find();
		if ($this->isPost()) {
			$oldpassword = isset ($_POST['oldpassword']) ? trim($_POST['oldpassword']) : '';
			if ($datainfo['password'] == md5($oldpassword)) {
				$data['password'] = isset ($_POST['password']) ? trim($_POST['password']) : '';
				$data['password'] = md5($data['password']);
				$mmember->where($map)->save($data);
				$this->success('密码修改成功！', 1);
			} else {
				$this->error('原始密码不一致！', 1);
			}

		} else {
			$this->assign('oldpassword', $datainfo['password']);
			$this->setSeo('修改密码');
			$this->display();
		}
	}
	public function reg() {
		if (!$this->isPost()) {
			$this->display();
		} else {
			$jumpurl = cookie('jumpurl');
			$mmember = D('Member');
			$member = $mmember->getform(1);
			Vendor('Valid');
			if (!Valid :: Name($member['username'], 'ALL', 5)) {
				$this->error('错误:会员名为5-20个字符，只能为字母、数字、下划线。',1);
			}
			if (!Valid :: Pass(6, 20, $member['password'])) {
				$this->error('错误:密码为6-20个字符，请使用字母、数字或其组合。',1);
			}
			if ($member['password'] != $_POST['repassword']) {
				$this->error('错误:两次输入密码不一致',1);
			}
			if (!Valid :: Email($member['email'])) {
				$this->error('错误:请输入有效的邮件地址。',1);
			}
			$verify = isset ($_POST['verify']) ? trim($_POST['verify']) : '';
			if (md5($verify) != Session :: get('verify')) {
				$msg = "请填写正确的验证码";
				$this->error($msg, 'verify');
			}
			$member['password'] = md5($member['password']);
			$map2['username'] = $member['username'];
			if ($mmember->where($map2)->find()) {
				$this->error('用户已经存在',1);
			}
			$member['union'] = 0;
			if ($mmember->relation(true)->add($member)) {
				$condition['username'] = $member['username'];
				$temp = $mmember->where($condition)->find();
				Session :: set('usertype', 'Member');
				Session :: set('member_id', (int) $temp['member_id']);
				Session :: set('username', $temp['username']);
				if ($jumpurl == '') {
					$this->success('注册成功',U('Member/index'));
					//redirect(U('Member/index'));
				} else {
					$this->success('注册成功',$jumpurl);
					//redirect($jumpurl);
				}
			} else {
				$this->assign("jumpUrl", U('reg'));
				$this->error('数据错误');
			}
		}
	}

	public function verify() {
		import("ORG.Util.Image");
		Image :: buildImageVerify();
	}
}
?>